package net.caixiaomi.info.ui.discovery;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.CommonFragmentPageAdapter;
import net.caixiaomi.info.adapter.LeagueHotAdapter;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.base.progressbar.CircularProgressBar;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.interfaces.MatchingSelectCallBack;
import net.caixiaomi.info.model.ActiveEntity;
import net.caixiaomi.info.model.ActiveListEntity;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.CommonTab;
import net.caixiaomi.info.model.LeagueDetailEntity;
import net.caixiaomi.info.model.LeagueDetailMatchListEntity;
import net.caixiaomi.info.model.LeagueDetailScoreListEntity;
import net.caixiaomi.info.model.LeagueGroupEntity;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.ui.football.detail.AnalysisFragment;
import net.caixiaomi.info.ui.view.HornorSelectView;
import net.caixiaomi.smartrefresh.layout.SmartRefreshLayout;
import net.caixiaomi.smartrefresh.layout.api.RefreshLayout;
import net.caixiaomi.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

/**
 * Created by mac on 2018/9/10.
 */

public class LeagueDetailActivity extends BaseActivity {

    private boolean unSelected=false;

    private String mLeagueId="";

    private List<String> titles;
    private Context mContext;
    public int mType=0;

    @BindView(R.id.league_season)
    TextView mSeasonTv;

    @BindView(R.id.title)
    TextView mTitle;

    private CommonFragmentPageAdapter mAdapter;
    LeagueScoreFragment fragment0;
    LeagueShooterFragment fragment1;
    LeagueDetailMatchFragment fragment2;
    LeagueDetailTeamFragment fragment3;
    @BindView(R.id.tab)
    TabLayout mTabLayout;
    @BindView(R.id.pager)
    ViewPager mViewPager;
    @BindView(R.id.appbar)
    AppBarLayout mAppBarLayout;
    @BindView(R.id.progress)
    CircularProgressBar mProgress;
    @BindView(R.id.container)
    View mContainer;
    @BindView(R.id.rule_title)
    TextView mRuleTitle;

    TextView ledBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTitle.setText("");
        mContainer.setVisibility(View.INVISIBLE);
        mContext=this;
        findViewById(R.id.frag_group).setFitsSystemWindows(true);
        mLeagueId=getIntent().getStringExtra("leagueId");

        String[] tabArray = getResources().getStringArray(R.array.TAB_LEAGUE_INFO);

        List<Fragment> data = new ArrayList<>();
        List<CommonTab> titles = new ArrayList<>();
        for (int index = 0; index < tabArray.length; index++) {
            CommonTab tab = new CommonTab(index, tabArray[index]);
            titles.add(tab);
            switch (index){
                case 0:
                    fragment0 = new LeagueScoreFragment();
                    break;
                case 1:
                    fragment1 = new LeagueShooterFragment();
                    break;
                case 2:
                    fragment2 = new LeagueDetailMatchFragment();
                    break;
                case 3:
                    fragment3 = new LeagueDetailTeamFragment();
                    break;
            }

        }
        data.add(fragment0);
        data.add(fragment1);
        data.add(fragment2);
        data.add(fragment3);

        mAdapter = new CommonFragmentPageAdapter(getSupportFragmentManager(), titles, data);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(4);
        mTabLayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来。
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        LinearLayout linearLayout = (LinearLayout) mTabLayout.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this, R.drawable.layout_divider_vertical));
//        mViewPager.addOnPageChangeListener(mChangeListener);
        mViewPager.setCurrentItem(0);

        mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (verticalOffset == 0) {//展开
                    mToolbar.setBackgroundColor(Color.argb((int) 0, 0, 0, 0));
                } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {//折叠
                    mToolbar.setBackgroundColor(ContextCompat.getColor(CommonApp.getInstance(), R.color.background));
                } else {//中间状态
                    mToolbar.setBackgroundColor(Color.argb((int) 0, 0, 0, 0));
                }
            }
        });

        loadData();
    }

    @Override
    protected int layout() {
        return R.layout.league_detail_main_layout;
    }

    private void loadData(){
        unSelected=false;
        JSONObject params = new JSONObject();
        params.put("leagueId", mLeagueId);
        params.put("seasonId", "0");
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<LeagueDetailEntity>> dis = requestService.getDiscoveryLeagueDetail(params);
        dis.enqueue(new ResponseCallback<BaseCallModel<LeagueDetailEntity>>() {
            @Override
            public void onSuccess(BaseCallModel<LeagueDetailEntity> data) {
                mProgress.setVisibility(View.GONE);
                mContainer.setVisibility(View.VISIBLE);
                bindData(data.data);
                fragment0.initData(data.data.getLeagueScore());
                fragment1.initData(data.data.getLeagueShooter());
                fragment2.initData(data.data.getMatchGroupData());
                fragment3.initData(data.data.getLeagueTeam());
            }

            @Override
            public void onFail(ResponseError error) {
                mProgress.setVisibility(View.GONE);
            }

            @Override
            public void onError() {
                mProgress.setVisibility(View.GONE);
            }
        });
    }

    private void bindData(final LeagueDetailEntity entity){
        mTitle.setText(entity.getLeagueAddr());
        mRuleTitle.setText(entity.getLeagueType()==1?"联赛规则":"杯赛规则");
        mSeasonTv.setText(entity.getLeagueSeason().getLeagueSeasonInfoList().get(0).getMatchSeason());
        ledBtn= (TextView) findViewById(R.id.league_des_btn);
        final TextView lDesTv= (TextView) findViewById(R.id.league_des);
        lDesTv.setText(entity.getLeagueRule());
        ledBtn.setVisibility(View.INVISIBLE);
        lDesTv.post(new Runnable() {
            @Override
            public void run() {
                int count=lDesTv.getLineCount();
                if(count>7){
                    lDesTv.setMaxLines(7);
                    ledBtn.setVisibility(View.VISIBLE);
                }
            }
        });


        if(entity.getLeagueRule().equals("None")|| TextUtils.isEmpty(entity.getLeagueRule())){
            lDesTv.setText("");
            ledBtn.setVisibility(View.INVISIBLE);
        }
        TextView lTitleTv= (TextView) findViewById(R.id.league_detail_title);
        lTitleTv.setText(entity.getLeagueAddr());
        ImageView lLogo= (ImageView) findViewById(R.id.league_detail_img);
        Glide.with(lLogo.getContext()).load(entity.getLeaguePic()).into(lLogo);

        if(unSelected){
            lDesTv.setMaxLines(100);
            ledBtn.setText("收起");

        }else{
            lDesTv.setMaxLines(7);
            ledBtn.setText("详情");
        }
        ledBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(unSelected){
                    lDesTv.setMaxLines(7);
                    ledBtn.setText("详情");
                }else{
                    lDesTv.setMaxLines(100);
                    ledBtn.setText("收起");
                }
                unSelected=!unSelected;
            }
        });
    }

}
